using System.Windows;
using Silvery.Data;
using Silvery;
using System.Collections.Generic;
using System;

namespace Silvery.Data
{
    public class SpiritFile_Frames : NTableDataWriter
    {
        public Dictionary<string, string> Attributes { get; private set; }
        public const string FileName = "Frames.txt";
        public const string FrameFile = "Frame";
        public const string OffsetX = "OffsetX";
        public const string OffsetY = "OffsetY";
        public const string AniName = "AniName";
        public const string Other = "Other";
        public const string Millisecondconst = "Millisecond";
        public NSpiritData SpiritData { get; set; }
        public int Millisecond
        {
            get
            {                
                if (datadic.ContainsKey(Millisecondconst) == false)
                {
                    datadic.Add(Millisecondconst, CreateAttribute(Millisecondconst, "33"));
                }
                return Convert.ToInt32(datadic[Millisecondconst][Other]);
            }
            set
            {
                Attributes[Millisecondconst] = value.ToString();
                if (datadic.ContainsKey(Millisecondconst) == false)
                {
                    datadic.Add(Millisecondconst, CreateAttribute(Millisecondconst, value.ToString()));
                }
                else
                {
                    datadic[Millisecondconst][Other] = value.ToString();
                }
            }
        }
        public List<string> FrameNames
        {
            get
            {
                return new List<string>(base.datadic.Keys);
            }
        }
        public SpiritFile_Frames()
        {
            Attributes = new Dictionary<string, string>();
            Attributes.Add(Millisecondconst, "33");
        }
        public override void Save(System.IO.Stream filestream)
        {
            if (SpiritData == null)
            {
                MessageBox.Show("保存失败，数据未设置");
                return;
            }
            int i = 0;
            datadic.Clear();

            foreach (var item in Attributes)
            {
                var info = CreateAttribute(item.Key, item.Value);
                datadic.Add(item.Key, info);
            }
            foreach (var item in SpiritData)
            {
                foreach (var frame in item.Value)
                {
                    var d = new Dictionary<string, string>();
                    var f = i + ".png";
                    d.Add(FrameFile, f);
                    if (frame.Tag == null)
                    {
                        d.Add(OffsetX, "0");
                        d.Add(OffsetY, "0");
                    }
                    else
                    {
                        d.Add(OffsetX, ((Point)frame.Tag).X.ToString());
                        d.Add(OffsetY, ((Point)frame.Tag).Y.ToString());
                    }
                    d.Add(AniName, item.Key);
                    d.Add(Other, "");
                    if (!datadic.ContainsKey(f))
                        this.datadic.Add(f, d);
                    i += 1;
                }
            }
            base.Save(filestream);
        }
        private Dictionary<string, string> CreateAttribute(string key, string value)
        {
            var info = new Dictionary<string, string>();
            info.Add(FrameFile, key);
            info.Add(Other, value);
            info.Add(OffsetX, "");
            info.Add(OffsetY, "");
            info.Add(AniName, "");
            return info;
        }
    }

}
